-- mechanical_area_damage
-- create by xinj
-- 机械装置范围伤害

return {
    apply = function(source, target, skillId, round, para, extra)
        local force = para[1];
        local level = 0;
        local radius = 0;

        -- 机械师专属加强效果
        local prop = PropM.combine(source, "mechanics", 1);
        level = PropM.apply(prop, level);
        if prop[3] > 0 then
            -- 触发特技发动事件
            EventMgr.fire(event.SPECIAL_SKILL_TRIGGER, { propId = prop[1] });
        end

        if level >= 1 then
            radius = radius + 1;
        end
        if level >= 2 then
            force = force + 30;
        end
        if level >= 3 then
            radius = radius + 1;
        end
        if level >= 4 then
            force = force + 60;
        end
        if level >= 5 then
            radius = radius + 1;
        end

        -- 遍历怪物周围所有已开且存活的怪
        local pos = target.dbase:query("pos");
        if not pos then
            pos = target:getOwner();
        end
        local coor = DungeonM.convertToCoor(pos);
        local ret = {};

        for p = 1, DUNGEON_WIDTH * DUNGEON_HEIGHT do
            local c = DungeonM.convertToCoor(p);

            -- 有效范围内
            local near = math.abs(c.x - coor.x) + math.abs(c.y - coor.y) <= radius;

            local grid = DungeonM.getGridByPos(p);

            -- 翻开且存活的怪物
            if  near and grid:isOpened() and
                grid:isMonster() and
                not grid.monster:isDead() then
                table.insert(ret, grid.monster);
            end
        end

        local damage = force;

        -- 道具伤害加成
        prop = PropM.combine(source, "property_damage", 1);
        damage = PropM.apply(prop, damage);

        local addon = 0;
        local args  = {["value"] = damage, ["class_id"] = extra.propertyId,};

        -- 概率造成额外的固定伤害
        local props = PropM.fetchProps(source, "prob_fixed_damage");
        for _, prop in ipairs(props) do
            addon = addon + PropM.trigger(source, prop[1], prop[2], args);
        end

        damage = damage + addon;

        for _, monster in pairs(ret) do
            -- 增加命中动作
            if SkillM.getSequence(monster) == nil then
                SkillM.getSequenceList()[monster] = CastSequence.create(source, monster, skillId);
            end

            SkillM.getSequence(monster):hit(source, monster, skillId);

            -- 道具修正伤害
            local classId = extra["propertyId"];
            local fixDamage = damage;
            if type(classId) == "number" then
                fixDamage = PropertyM.fixPropertyDamage(source, monster, classId, fixDamage);
            end

            -- 受创
            CombatM.receiveDamage(source, monster, fixDamage, skillId, extra);
        end

        -- 如果受攻击对象是怪物，且由道具触发技能，抛出投掷类攻击事件
        if target.type == OBJECT_TYPE_MONSTER and para[2] > 0 then
            if #ret == 1 then
                EventMgr.fire(event.PROPERTY_ATTACK, { ["classId"] = para[2], ["target"] = target, ["source"] = source});
            else
                table.removeItem(ret, target);
                EventMgr.fire(event.PROPERTY_ATTACK, { ["classId"] = para[2], ["target"] = target, ["source"] = source, ["moreTarget"] = ret});
            end
        end

        -- 作用目标
        return ret;
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        if (#arr >= 1) then
            return { tonumber(arr[1]), tonumber(arr[2]) or -1,};
        end
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        -- 无需技能描述
        return desc;
    end,
};
